<template>
  <el-form label-width="100px" label-position="left">
    <el-form-item label="图层名称" prop="layerName">
      <el-input
        v-model.trim="formData.layerName"
        size="default"
        style="width: 100%"
        clearable
        @change="changed($event, 'layerName')"
      />
    </el-form-item>

    <el-form-item label="样式" prop="showType">
      <el-select
        v-model="formData.showType"
        clearable
        placeholder="请选择"
        @change="(val) => changed(val, 'showType')"
      >
        <el-option
          v-for="item in showTypeOptions"
          :key="item.code"
          :label="item.name"
          :value="item.code"
        />
      </el-select>
    </el-form-item>
  </el-form>
</template>
<script lang="ts" setup>
import { reactive, watch } from 'vue'

// 接收父组件传入的值
const props = defineProps<{
  modelValue: any
}>()

const emit = defineEmits(['update:modelValue', 'onChanged'])

const formData = reactive<any>({})

const showTypeOptions = ref<any[]>([])

const createShowTypeOptions = () => {
  let selectOptions: any[] = []
  for (let i = 1; i < 14; i++) {
    selectOptions.push({ code: 'dv-border-Box-' + i, name: '样式' + i })
  }
  showTypeOptions.value = selectOptions
}

createShowTypeOptions()

watch(
  () => props.modelValue,
  (newVal) => {
    Object.assign(formData, newVal)
  },
  { immediate: true, deep: true }
)

const changed = (val: any, key: string) => {
  emit('update:modelValue', { ...formData })
  emit('onChanged', { key, val })
}
</script>
<style lang="scss" scoped></style>
